/**
 * author: Litpen
 * Date: 2007-11-22
 * an Ajax Object
 */

function Ajax(_sUrl,_sPara,_bAsync,_fCallback,_sReqType,_cbParamArray){
	this.sUrl = _sUrl;
	this.sPara = _sPara;
	this.bAsync = _bAsync;
	this.sReqType = _sReqType;	
	if (typeof Ajax._initialized == "undefined"){
		Ajax.prototype.createXmlHttp = function(){
			if (typeof XMLHttpRequest != "undefined"){
				return new XMLHttpRequest();
			}else if (window.ActiveXObject){
				var aVersions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
				for (var i = 0;i<aVersions.length;i++){
					try{
						var oXmlHttp = new ActiveXObject(aVersions[i]);
						return oXmlHttp;
					}catch(oError){
					}
				}
			}
			throw new Error("XMLHttp object could not be created.");
		}
		
		
		Ajax._initialized = true;
	}
	this.xmlHttp = this.createXmlHttp();
	this.send = function() {

			var oXmlHttp = this.xmlHttp;
			oXmlHttp.open(this.sReqType,this.sUrl,this.bAsync);
			if(_sReqType == "post") 
				oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			if (this.bAsync){
				oXmlHttp.onreadystatechange = function (){
					if (oXmlHttp.readyState == 4){
						var sContentType = oXmlHttp.getResponseHeader("Content-Type");
						if (sContentType == "text/xml"){
							_fCallback(oXmlHttp.responseXML,_cbParamArray);
						}else if (sContentType == "text/plain"){
							_fCallback(oXmlHttp.responseText,_cbParamArray);
						}else{
							if (oXmlHttp.status == 200){

								_fCallback(oXmlHttp.responseText,_cbParamArray);
							}else{
								alert("An error occurred: " + oXmlHttp.statusText);
							}
						}
					}else{
					
					}
				}
			}
			if(window.ActiveXObject && _sReqType == "get"){
				oXmlHttp.send();
			} 	
			else{
				oXmlHttp.send(_sPara);
			}
		}
}

function formToRequestString(form_obj)
{

	var query_string='';
	var and='';

	for (i=0;i<form_obj.length ;i++ )
	{
		
		e=form_obj[i];
		if (e.name!='')
		{
			if (e.type=='select-one')
			{
				element_value=e.options[e.selectedIndex].value;
			}
			else if (e.type=='checkbox' || e.type=='radio')
			{
				if (e.checked==false)
				{
					continue;	
				}
				element_value=e.value;
			}
			else if (e.type=='submit' || e.type=='reset')
			{
				continue;
			}
			else 
			{
				element_value=e.value;
			}
			query_string+=and+e.name+'='+element_value.replace("+","%2b").replace(/\&/g,"%26");
			and="&"
		}
		
	}
	return query_string;
}